package com.lu.oss.tools

import android.util.Log
import java.io.File
import java.io.FileInputStream

/**
 *author: luqihua
 *date:2021/11/24
 *description:
 **/
object CRC64Tools {
    private const val TAG = "CRC64Tools"

    /**
     * Get file CRC64 value
     * @param file
     * @return
     */
    @JvmStatic
    fun getFileCRC64Value(file: File): Long {
        val fis = FileInputStream(file)

        val startTime = System.currentTimeMillis()
        Log.d(TAG, "getFileCRC64Value ${file.absolutePath}: start $startTime")
        val crc64 = CRC64Turbo()

        val buffer = ByteArray(1024)
        var len = fis.read(buffer)
        while (len > 0) {
            crc64.update(buffer, 0, len)
            len = fis.read(buffer)
        }
        fis.close()
        val endTime = System.currentTimeMillis()
        Log.d(
            TAG,
            "getFileCRC64Value ${file.absolutePath}: end  time = ${endTime - startTime}  crc= ${crc64.value}"
        )
        return crc64.value
    }
}